<%@page import="cn.example.freid.util.ResultsetUtil"%>
<%@page import="cn.example.freid.domain.ExampleDomain"%>
<%@page import="java.util.List"%>
<%@page import="cn.example.freid.util.SQLStrings"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="cn.example.freid.util.JDBCUtil"%>
<%@page import="cn.example.freid.util.StringUtil"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%
	
	String id = request.getParameter("id");
	ExampleDomain domain = null;
	
	if (!StringUtil.isNullOrEmpty(id)) {
		JDBCUtil jdbcUtil = new JDBCUtil();
		ResultSet rs = jdbcUtil.query(SQLStrings.QUERY_BYID_SQL, id);
		List<ExampleDomain> list = ResultsetUtil.resultsetToList(rs, ExampleDomain.class);
		if (list.size() > 0)
	domain = list.get(0);
	}
%>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="fonts/favicon.ico">

    <title>Sign Admin</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/index.css" rel="stylesheet">

	<script>
	
		var fName = "";
		var fMail = "";
		var fID = "";
		var fComments = "";
		<%if (domain != null) {%>
		fName = "<%= domain.getName() %>";
		fMail = "<%= domain.getMail() %>";
		fID = "<%= domain.getID() %>";
		fComments = "<%= domain.getComments() %>";
		<%}%>
		
		function doSubmit(method) {
			
			$("#loadingImg").show();
			
			var urlString = ""; 
			if (method == "new") {
				urlString = "./Add.do";
			} else {
				urlString = "./Mod.do";
			}
			$.ajax({
	    			url : urlString,
	    			dataType : "json",
	    			type : "POST",
	    			data : $("#infoForm").serialize(),
	    			cache : false,
	    			error : function(XMLHttpRequest, textStatus, errorThrown) {
	     				alert(XMLHttpRequest.responseText + errorThrown);
	    			},
	    			success : function(data) {
	    				if (data.error == 0) {
	    					$("#msgDialog").show();
	    					$("#loadingImg").hide();
	    					fName = $("#_name").val();
							fMail = $("#_mail").val();
							fID = $("#_id").val();
							fComments = $("#_comments").val();
	    				} else {
	    					alert(data.msg);
	    					$("#loadingImg").hide();
	    				}
	    			}
	    		});
    			return false;
		}
		
		function doReset() {
			
			$("#_name").val(fName);
			$("#_mail").val(fMail);
			$("#_id").val(fID);
			$("#_comments").val(fComments);
			
			return false;
		}
		
		function closeMsg() {
    		$("#msgDialog").hide();
    		return false;
    	}
	</script>
  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.jsp">Sign Admin</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Help</a></li>
          	<li><a href="./AdminLogOut.do">Log out</a></li>
          </ul>
        </div>
      </div>
    </nav>

    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            <li class="active"><a href="#">Information</a></li>
          </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header"> information</h1>
          <div class="table-responsive">
			  <form class="form-horizontal" role="form" style="max-width:400px; margin-bottom:30px" id="infoForm" 
			  		onSubmit="doSubmit(<%=domain==null?"'new'":"'mod'"%>);return false;">
			    <fieldset>
			    	  <!-- Text input-->
			          <label class="control-label" for="input01">ID</label>
			          <div class="controls">
			            <input type="text" name="domainID" id="_id" 
			            <%if (domain != null) {%>
			             value="<%=domain.getID() %>" readonly="readonly"
			            <%} %>
			             placeholder=" ID" class="form-control" required>
			            <p class="help-block"></p>
			          </div>
			    	  <!-- Text input-->
			          <label class="control-label" for="input01">Mail</label>
			          <div class="controls">
			            <input type="email" name="domainMail" id="_email" 
			            <%if (domain != null) {%>
			             value="<%=domain.getMail() %>"
			            <%} %>
			            placeholder="mail" class="form-control" required>
			            <p class="help-block"></p>
			          </div>
			          <!-- Text input-->
			          <label class="control-label" for="input01">Name</label>
			          <div class="controls">
			            <input type="text" name="domainName" id="_name" 
			            <%if (domain != null) {%>
			             value="<%=domain.getName() %>" 
			            <%} %>
			            placeholder=" Name" class="form-control" required>
			            <p class="help-block"></p>
			          </div>
			          <!-- Textarea -->
			          <label class="control-label">Comments</label>
			          <div class="controls">
			            <div class="textarea">
			                  <textarea style="margin: 0px; width: 279px; height: 53px;" 
			                  id="_comments" name="comments"><%=domain==null?"":domain.getComments() %></textarea>
			            </div>
			          </div>
			          <label class="control-label"></label>
			          <div style="margin-bottom:20px" id="loadingImg">
			          	<img width="40" height="40" src="img/loading.gif">
			          </div>
			          <!-- Button -->
			          <div class="controls">
			            <button class="btn btn-primary" type="submit">Submit</button>
			            <button class="btn btn-info" type="button" onclick="doReset();return false">Reset</button>
			            <button class="btn btn-default" type="button" onclick="location.href='index.jsp'">Cancel</button>
			          </div>
			
			    </fieldset>
			  </form>
          </div>
          <div class="alert alert-success alert-dismissible" role="alert" id="msgDialog">
			  	<button type="button" class="close" id="msgCloseBtn" onclick="closeMsg()"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
			  	<strong>Success!</strong> Submitted successfully.
			  	</div>
          </div>
      </div>
    </div>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="js/jquery-1.11.1.js"></script>
    <script src="js/bootstrap.js"></script>
    <script>
    	$("#msgDialog").hide();
    	$("#loadingImg").hide();
    </script>
  </body>
</html>
